package com.tmq.diancan.granter;

import com.tmq.diancan.entity.User;
import com.tmq.diancan.entity.UserInfo;
import com.tmq.diancan.entity.WxUser;
import com.tmq.diancan.service.IWxUserService;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.OkHttpUtil;
import org.springblade.core.tool.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author tangmqb DCITS
 * @DATE 2021/3/4 14:32
 * @Version 1.0.0
 */
@Component
@AllArgsConstructor
public class WxTokenGranter implements ITokenGranter {
    public static final String GRANT_TYPE = "weixin";
    public static final String JSCODE_TO_SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session";
    @Autowired
    private IWxUserService wxUserService;
    @Override
    public UserInfo grant(TokenParameter tokenParameter) {
        //性别
        String gender = tokenParameter.getArgs().getStr("tenantId");
//        头像地址
        String avatar = tokenParameter.getArgs().getStr("account");
//        头像点击名称
        String nickName = tokenParameter.getArgs().getStr("password");
//        授权码
        String code = tokenParameter.getArgs().getStr("refreshToken");
        Map<String, String> params = new HashMap(8);
        params.put("appid", "wxb16152436f445fe8");
        params.put("secret","a90ac93846354aa86b622a3521b6749f");
        params.put("js_code", code);
        params.put("grant_type", "authorization_code");
        String result = OkHttpUtil.get(JSCODE_TO_SESSION_URL, params);
        Map<String, Object> resMap = JsonUtil.toMap(result);
        System.out.println(result);
        UserInfo userInfo = new UserInfo();
        User user = new User();
        user.setPassword((String)resMap.get("openid"));
        user.setPhone((String)resMap.get("session_key"));
        user.setId(333);
        user.setRoleId("1");
        user.setAccount(nickName);
        user.setAvatar(avatar);
        user.setName(nickName);
        user.setRealName(nickName);
        user.setSex(Integer.parseInt(gender));
        userInfo.setUser(user);

        WxUser wxUser = new WxUser();
        wxUser.setUserName((String)resMap.get("openid"));
        wxUser.setPassword((String)resMap.get("openid"));
        wxUser.setWxOpenid((String)resMap.get("openid"));
        List<WxUser> listExit = wxUserService.list(Condition.getQueryWrapper(wxUser));
        wxUser.setWxAvatar(avatar);
        wxUser.setWxNickname(nickName);
        wxUser.setGender(gender);
        wxUser.setLastLoginIp(WebUtil.getIP());
        wxUser.setWxSessionKey((String)resMap.get("session_key"));
        if (Func.isEmpty(listExit)) {
            wxUserService.save(wxUser);
        }else {
            wxUser.setUpdateTime(DateUtil.now());
            wxUserService.updateLoginTime(wxUser);
        }
        return userInfo;
    }
}
